/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ircserver;
import java.io.IOException;
import java.util.Scanner;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
 *
 * @author Justin
0 */
public class AddPerson extends Thread {
    private Socket s;
    private PrintWriter out;
    private Scanner in;
    private Users users;
    public AddPerson(Socket s,Users users)
    {
        System.out.println("Client connected.");
        this.s=s;
        this.users=users;
    }
    @Override
    public void run()
    {
        try {
            try {
                out =new PrintWriter(s.getOutputStream());
                in= new Scanner(new InputStreamReader(s.getInputStream()));
                users.add(out);
                getInput();
            } finally {
                users.remove(out);
                s.close();
            }
        } catch (Exception exception) {}
    }
    public void getInput() throws IOException
    {
        while (true)  {
           if(!in.hasNext())
            {
                return;
            }
           users.writeInput(in.nextLine());
        }
    }
}
